package com.hp.appfw.service.cust;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.hp.appfw.data.mapper.gent.BaseMessageLogExMapper;
import com.hp.appfw.data.mapper.mygen.BaseMessageLogMapper;
import com.hp.appfw.data.mode.mygen.BaseMessageLog;
import com.hp.appfw.data.mode.mygen.BaseMessageLogExample;

@Service("msgLogService")
public class MsgLogService {
	@Resource
	private BaseMessageLogMapper baseMessageLogMapper;
	@Resource
	private BaseMessageLogExMapper baseMessageLogExMapper;


	public void insert(BaseMessageLog item)
	{
		item.setDeleteFlag("False");
		item.setDataVersion(0);
		item.setCreatedBy("system");
		item.setUpdatedBy("system");
		item.setCreateTime(new Date());
		item.setUpdateTime(new Date());
		
		baseMessageLogMapper.insert(item);
	}
	public void delete(String id)
	{
		baseMessageLogMapper.deleteByPrimaryKey(id);
	}
	public void update(BaseMessageLog item)
	{
		item.setDataVersion(item.getDataVersion() + 1);
		item.setUpdatedBy("system");
		item.setUpdateTime(new Date());
		
		baseMessageLogMapper.updateByPrimaryKey(item);
	}
	public BaseMessageLog getItem(String id)
	{
		BaseMessageLogExample example = new BaseMessageLogExample();
		example.createCriteria().andIdEqualTo(id).andDeleteFlagEqualTo("False");
		List<BaseMessageLog> list = baseMessageLogMapper.selectByExample(example);
		if(list.size() > 0)
			return list.get(0);
		else
			return null;
	}
	public List<BaseMessageLog> selectByKey(
			  String key
			, String missionId
			, String type
			, String recieverId
			, Date createTimeMin
			, Date createTimeMax
			, String entId
			, int start
			, int num) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();

		map.put("entId", entId);

		if(key != null && key.trim().length() > 0)
			map.put("key", key + "%");
		else
			map.put("key", null);

		if(start >=0 && num >0 )
		{
			map.put("start", new Integer(start));
			map.put("end", new Integer(start + num));
		}
		
		if(missionId != null)
			map.put("missionId", missionId);

		if(type != null)
			map.put("type", type);

		if(recieverId != null)
			map.put("recieverId", recieverId);

		if(createTimeMin != null)
			map.put("createTimeMin", createTimeMin);

		if(createTimeMax != null)
			map.put("createTimeMax", createTimeMax);

		return baseMessageLogExMapper.pageSelect(map);
	}

	public int countByKey(
			  String key
			, String missionId
			, String type
			, String recieverId
			, Date createTimeMin
			, Date createTimeMax
			, String entId) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		
		map.put("entId", entId);

		if(key != null && key.trim().length() > 0)
			map.put("key", key + "%");
		else
			map.put("key", null);

		if(missionId != null)
			map.put("missionId", missionId);

		if(type != null)
			map.put("type", type);

		if(recieverId != null)
			map.put("recieverId", recieverId);

		if(createTimeMin != null)
			map.put("createTimeMin", createTimeMin);

		if(createTimeMax != null)
			map.put("createTimeMax", createTimeMax);

		return baseMessageLogExMapper.pageCount(map);
	}
}
